package com.emex.app.stuff.provider;

import com.emex.app.shared.data.Client;
import com.emex.app.shared.data.JuridicalPerson;
import com.emex.app.shared.ws.ClientWebService;
import com.emex.app.stuff.utils.WebServiceHelper;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;

/**
 *
 * @author Sergey Karavaev
 */
public class ClientDataProvider extends Service {

    private ClientWebService service = null;

    private static ClientDataProvider instance = null;

    /**
     * Constructor
     */
    private ClientDataProvider() throws MalformedURLException {
        super(new URL(WebServiceHelper.getDefault().getWebServiceURL("web", "Client")),
                WebServiceHelper.getDefault().getWebServiceQName("Client"));
        service = getPort(ClientWebService.class);
    }

    /**
     * Get instance of data provider.
     *
     * @return data provider instance
     */
    public static synchronized ClientDataProvider getInstance() {
        if (instance == null) {
            new RequestWebService() {
                @Override
                protected void doRequestBody() throws MalformedURLException {
                    instance = new ClientDataProvider();
                }
            }.doRequest();
        }
        return instance;
    }

    public BindingProvider getBindingProvider() {
        return (BindingProvider) service;
    }

    public int deleteByPrimaryKey(Integer id) {
        return service.deleteByPrimaryKey(id);
    }

    public Client insert(Client record) {
        return service.insert(record);
    }

    public List<Client> selectByPhoneOrSurname(String criteria) {
        return service.selectByPhoneOrSurname(criteria);
    }

    public Client selectByPrimaryKey(Integer id) {
        return service.selectByPrimaryKey(id);
    }

    public Client updateByPrimaryKey(Client record) {
        record = service.updateByPrimaryKey(record);
        return record;
    }

    public List<Client> getAllClients() {
        return service.getAllClients();
    }

    public List<Client> getAllClientsByOrderPositionName(String name) {
        return service.getAllClientsByOrderPositionName(name);
    }

    public List<JuridicalPerson> getAllJuridicalClientsByOrderPositionName(String name) {
        return service.getAllJuridicalClientsByOrderPositionName(name);
    }
    
 }
